/**
 * public Model类
 * @author 冯永泽
 * @date 2019/12/6
 */
const config = require('../config');
const mongo = require('../utils/mongo-utils');
let mongoClient = new mongo(config.dbUrl(), 'machine');
/**
 *
 *
 * @class Dao
 */
class Dao {
    constructor() {
        
    }
    /**
     *
     * 插入order数据库
     * @param {*} arr
     * @param {*} cb
     * @memberof Dao
     */
    insertOrder(arr, cb = () => {}) {
        mongoClient.insert('order', arr, (res) => {
            cb(res);
        });
    }
    /**
     *
     * 更新order数据库
     * @param {*} where
     * @param {*} obj
     * @param {function} cb 回调函数
     * @memberof Dao
     */
    updateOrder(where, obj, cb = () => {}) {
        mongoClient.update('order', where, obj, (res) => {
            cb(res);
        });
    }
    /**
     * 获取未处理订单
     * @param {function}
     * @memberof Dao
     */
    findReadyOrder(cb = () => {}) {
        mongoClient.find('order', {status: 1}, (res) => {
            cb(res);
        });
    }
    /**
     * 按条件查询订单
     * @param {function}
     * @memberof Dao
     */
    findAllOrder(params,cb = () => {}) {
        mongoClient.find('order', params, (res) => {
            cb(res);
        });
    }
    /**
     *
     * 通过日期和id获取菜品订单
     * @param {Number} id 当天Id
     * @param {String} date 日期
     * @param {Function} cb
     * @memberof Dao
     */
    findOrderById(id, date,cb = () => {}) {
        mongoClient.find('order', {todayId: id, date: date}, (res) => {
            cb(res);
        });
    }
    /**
     *
     * 更新菜品订单状态
     * @param {*} id 查询的菜品ID
     * @param {*} status 更新状态
     * @param {*} cb 回调函数
     * @memberof Dao
     */
    updateOrderStatus(id, status, cb = () => {}) {
        mongoClient.update('order', {_id: id}, {status: status, uTime: parseInt(new Date().getTime()/1000)} , res => {
            cb(res);
        })
    }
    /**
     *
     * 查找最新订单
     * @param {*} cb
     * @memberof Dao
     */
    findOrder(query, cb = () => {}) {
        mongoClient.findOne('order',query,{sort: {'_id': -1}} ,res => {
            cb(res);
        })
    }
    /**
     *
     * 查找空闲订单
     * @param {Function} cb 回调函数
     * @memberof Dao
     */
    findFreeOrder(query, cb = () => {}) {
        mongoClient.findOne('order', query, {sort: {'_id': 1}}, res => {
            cb(res);
        })
    }
   
    /**
     *
     * 查找错误订单
     * @param {Function} cb 回调函数
     * @memberof Dao
     */
    findErrorOrder(cb) {
        mongoClient.find('order', {'status': 1},res => {
            cb(res);
        });
    }
}

module.exports = Dao;